<!-- components/Form/index.vue -->
<template>
  <div>
    <el-form class="Form" :model="formData" :rules="rules" ref="ruleForm" :label-width="labelWidth"
      :label-position="labelPosition">
      <div class="formList">
        <slot name="searchform" />
        <el-form-item v-for="item in formConfig" :label="item.label" :prop="item.prop" :key="item.label">
          <!-- 输入框 -->
          <el-input v-if="item.type === 'input'" v-model="formData[item.prop]" :disabled="item.disabled" placeholder="请输入"
            :style="{ width: item.width }" @change="item.change && item.change(formData[item.prop])" clearable></el-input>

          <!-- 密码框 -->
          <el-input v-if="item.type === 'password'" type="password" v-model="formData[item.prop]" autocomplete="off"
            placeholder="请输入" :style="{ width: item.width }" @change="item.change && item.change(formData[item.prop])"
            clearable show-password></el-input>

          <!-- 文本域 -->
          <el-input v-if="item.type === 'textarea'" type="textarea" v-model="formData[item.prop]" placeholder="请输入"
            :disabled="item.disabled" :style="{ width: item.width }" :maxlength="item.maxlength" :rows="item.rows"
            :show-word-limit="item.showWordLimit" @change="item.change && item.change(formData[item.prop])"></el-input>

          <!-- 下拉框 -->
          <el-select v-if="item.type === 'select'" filterable v-model="formData[item.prop]" :disabled="item.disabled"
            placeholder="请选择" :style="{ width: item.width }" @change="item.change && item.change(formData[item.prop])"
            clearable>
            <el-option v-for="option in item.options" :label="option.label" :value="option.value"
              :key="option.value"></el-option>
          </el-select>

          <!-- 单选框 -->
          <el-radio-group v-if="item.type === 'radio'" v-model="formData[item.prop]" :disabled="item.disabled"
            @change="item.change && item.change(formData[item.prop])">
            <el-radio v-for="radio in item.radios" :label="radio.label" :key="radio.label"></el-radio>
          </el-radio-group>

          <!-- 复选框 -->
          <el-checkbox-group v-if="item.type === 'checkbox'" v-model="formData[item.prop]" :disabled="item.disabled"
            @change="item.change && item.change(formData[item.prop])">
            <el-checkbox v-for="checkbox in item.checkboxs" :label="checkbox.label" :key="checkbox.label"></el-checkbox>
          </el-checkbox-group>

          <!-- 开关 -->
          <el-switch v-if="item.type === 'switch'" v-model="formData[item.prop]" :disabled="item.disabled"
            @change="item.change && item.change(formData[item.prop])"></el-switch>

          <!-- 上传 -->
          <el-upload v-if="item.type === 'upload'" class="avatar-uploader" ref="uploadxls" :action="item.action"
            :limit="item.limit" :show-file-list="false" :on-success="handleSuccess" :on-change="handleFileChange"
            :before-upload="beforeUpload">
            <slot name="upload"></slot>
          </el-upload>
          <!-- 级联 -->
          <el-cascader v-if="item.type === 'cascader'" size="large" v-model="formData[item.prop]" :options="options"
            placeholder="请选择" :style="{ width: item.width }" @change="item.change && item.change(formData[item.prop])"
            clearable>
          </el-cascader>

          <!-- 时间date -->
          <el-date-picker v-if="item.type === 'date'" v-model="formData[item.prop]" type="date" value-format="yyyy-MM-dd"
            :style="{ width: item.width }" @change="item.change && item.change(formData[item.prop])" clearable>
          </el-date-picker>

          <!-- 时间daterange -->
          <el-date-picker v-if="item.type === 'daterange'" v-model="formData[item.prop]" type="daterange"
            value-format="yyyy-MM-dd" :style="{ width: item.width }"
            @change="item.change && item.change(formData[item.prop])" clearable>
          </el-date-picker>
        </el-form-item>
      </div>
      <!-- 操作按钮 -->
      <div class="slotList" v-if="slotList.length > 0">
        <div v-for="slotItem, index in slotList" :key="index" style="padding: 0 5px;">
          <slot :name="slotItem.slotName" />
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
import Vue from 'vue';

export default Vue.extend({
  props: {
    slotList: {
      type: Array,
      required: false
    },
    // 表单域标签的宽度
    labelWidth: {
      type: String,
      default: '100px'
    },
    // 表单域标签的位置
    labelPosition: {
      type: String,
      default: 'right'
    },
    // 表单配置
    formConfig: {
      type: Array,
      required: true
    },
    // 表单数据
    formData: {
      type: Object,
      required: true
    },
    // 表单规则
    rules: {
      type: Object
    },
    // 级联数据
    options: {
      type: Array
    }
  },
  methods: {
    // 上传成功的方法
    handleSuccess (res, file) {
      this.$emit('uploadSuccess');
      console.log('ok');
    },
    // 文件状态改变时的方法
    handleFileChange (file, fileList) {
      // URL.createObjectURL(file.raw) 一般在上传成功的函数中，这里是为了测试方便
      this.$emit('uploadFileChange', URL.createObjectURL(file.raw));
      console.log(file, fileList);
    },
    // 上传文件之前的方法
    beforeUpload (file) {
      this.$emit('uploadbefore', file);
      console.log(file);
      // return false;
    },
    // 表单字段校验方法(自定义检验)
    validateField (valid) {
      this.$refs.ruleForm.validateField(valid);
    },
    // 提交判断方法
    submitForm (callback) {
      this.$refs.ruleForm.validate(valid => {
        callback(valid);
      });
    },
    // 重置方法
    resetForm () {
      this.$refs.ruleForm.resetFields();
    }
  }
});
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

::v-deep.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

::v-deep textarea {
  resize: none;
}

.Form {
  display: flex;
  justify-content: space-between;
}

.slotList {
  padding: 0 10px;
  display: flex;
  justify-content: space-between;
  flex-wrap: nowrap;

  .el-form-item__content {
    width: 0;
  }
}

/* 左侧区域 */
.formList {
  height: 100%;
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  padding:10px ;

  .el-form-item--small.el-form-item {
    margin-bottom: 10px;
  }
}
</style>


